package com.cws.project.cws.graduation.audit.service.impl;

import com.cws.common.utils.DateUtils;
import com.cws.common.utils.StringUtils;
import com.cws.common.utils.text.Convert;
import com.cws.project.cws.course.domain.Course;
import com.cws.project.cws.course.service.ICourseService;
import com.cws.project.cws.graduation.audit.domain.GraduationAudit;
import com.cws.project.cws.graduation.audit.mapper.GraduationAuditMapper;
import com.cws.project.cws.graduation.audit.service.IGraduationAuditService;
import com.cws.project.cws.graduation.project.domain.GraduationProject;
import com.cws.project.cws.homework.service.IHomeworkService;
import com.cws.project.cws.score.mapper.HomeworkScoreMapper;
import com.cws.project.cws.score.service.IHomeworkScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * 毕设内容Service业务层处理
 *
 * @author xyl
 * @date 2022-01-19
 */
@Service
public class GraduationAuditServiceImpl implements IGraduationAuditService {
    @Autowired
    private GraduationAuditMapper graduationAuditMapper;
    @Autowired
    private HomeworkScoreMapper homeworkScoreMapper;
    @Autowired
    private IHomeworkScoreService homeworkScoreService;
    @Autowired
    private ICourseService courseService;

    @Override
    public List<GraduationAudit> selectGraduationAuditList(GraduationAudit graduationAudit) {
        List<GraduationAudit> graduationAudits = graduationAuditMapper.selectGraduationAuditList(graduationAudit);
        //计算平均gpa，计算挂科数，首页报警
        for (GraduationAudit audit : graduationAudits) {
            Course course = new Course();
            course.setStudentId(audit.getStudentId());
            //所有课程信息
            List<Course> courses = courseService.selectStudentCourseList(course);
            //计算平均
            Double tempGpa = 0.00;
            Double tempScore = 0.00;
            Integer total = 0;
            Integer failScore = 0;
            boolean con = false;
            for (Course cours : courses) {
                if (StringUtils.equals(cours.getIsCourseEnd(),"是")) {
                    Double gpa = cours.getGpa();
                    Double score = cours.getScore();
                    if(score < 60){
                        failScore++;
                    }
                    tempGpa += gpa;
                    tempScore += score;
                    total++;
                    con = true;
                }
            }
            if(con){
                audit.setAvgGpa(new BigDecimal(tempGpa / total).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                audit.setScore(new BigDecimal(tempScore / total).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                audit.setFailScoreCount(failScore);
            }
        }
        return graduationAudits;
    }

    @Override
    public GraduationAudit selectGraduationAuditById(Long graduationProductId) {
        return graduationAuditMapper.selectGraduationAuditById(graduationProductId);
    }

    @Override
    public int updateGraduationAudit(GraduationAudit graduationAudit) {
        return graduationAuditMapper.updateGraduationAudit(graduationAudit);
    }
}
